$(function(){

	//初始化滚动区域
	initScroll(); 
	// 搜索框点击
	$('#searchBtn').on('tap',function(){
		var key = $('input').val().replace(/\s*/g,"");
		if(!key){
			mui.toast("请输入搜索内容",{duration:"short",type:"div"});
		}
		else {
			getSearchData({
				proName:key,
				page:1,
				pageSize:4 
			},
			function(data){ // 根据传过来的key加载数据
				$('.pro_list').html(template("proList",data))
			});
		}
	})

	// 
	var urlParams = LT.getParamsUrl();
	var $input = $('input').val(urlParams.key || '');
	

	// 排序
	$('.lt_sort a').on('tap',function(){
		if($(this).hasClass('active')){
			if($(this).children('span').hasClass('fa-angle-down')){
				$(this).children('span').removeClass('fa-angle-down').addClass('fa-angle-up');
			}
			else{
				$(this).children('span').removeClass('fa-angle-up').addClass('fa-angle-down');
			}
		}
		else {

			$(this).addClass('active').siblings().removeClass('active').find('span')
			.removeClass('fa-angle-up').addClass('fa-angle-down');;
		}

		var sort = $(this).attr("data-sort");
		var isRise = ($(this).children('span').hasClass("fa-angle-down")) ? 2 : 1;
		var params = {
			proName:$input.val,
			page:window.page,
			pageSize:4,
		}
		console.log(isRise,$(this).children('span').hasClass("fa-angle-down"));
		params[sort] = isRise;
		// 加载第一页前四条数据
		getSearchData(params,
		function(data){ // 根据传过来的key加载数据
			$('.pro_list').html(template("proList",data))
		});

	});


	// 下拉刷新
	mui.init({
	  pullRefresh : {
	    container:"#refreshContainer",//下拉刷新容器标识，querySelector能定位的css选择器均可，比如：id、.class等
	    down : {
	      // // style:'circle',//必选，下拉刷新样式，目前支持原生5+ ‘circle’ 样式
	      auto:true,
	      callback :function(){
	      	var that = this;
	      	$('.lt_sort a').removeClass('active').find('span').removeClass('fa-angle-up').addClass('fa-angle-down');
	      	getSearchData({
				proName:$input.val,
				page:1,
				pageSize:4,
	      	},function(data){
	      		setTimeout(function(){
					$('.pro_list').html(template("proList",data));
					that.endPulldownToRefresh();
					that.refresh(true);
					$(".loading").remove();
	      		},2000)
	      	});
	      } //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
	    },
	    up: {
	    	callback:function(){
	    		var that = this;
	    		window.page ++;

				var sort = $(".lt_sort a.active").attr("data-sort");
				var isRise = ($(".lt_sort a,active").children('span').hasClass("fa-angle-down")) ? 2 : 1;
				params = {
					proName:$input.val,
					page:window.page,
					pageSize:4,
	    		}
	    		params[sort] = isRise;
	    		getSearchData(params,function(data){
	    			setTimeout(function(){
	    				$('.pro_list').append(template("proList",data));
						that.endPullupToRefresh(data.data.length <= 0 ? true : false);
	    			},2000)
	    		})
	    	}
	    } 
	  }
	});

	$('#refreshContainer').on('tap',"a",function(){
		// console.log($(this).attr('href'));
		location.href = $(this).attr('href');
	});	

});

var rander = function(){
	 proList
}

var getSearchData = function(params,callback){
	$.ajax({
		url:"/product/queryProduct",
		type:'get',
		data:params,
		dataType:"json",
		success:function(res){
			window.page = res.page;
			console.log(window.page);
			callback && callback(res);
		}
	})
}
